﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace TransFormer.DAL
{
    /// <summary>
    /// 数据访问类:Product
    /// </summary>
    public partial class Product
    {
        public Product()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ProductId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Product");
            strSql.Append(" where ProductId=@ProductId");
            SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)
			};
            parameters[0].Value = ProductId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(TransFormer.Model.Product model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Product(");
            strSql.Append("ProductName,ProductDesc,PicUrl,Price,ProductNum,Unit,Size,Type,Remark,Language)");
            strSql.Append(" values (");
            strSql.Append("@ProductName,@ProductDesc,@PicUrl,@Price,@ProductNum,@Unit,@Size,@Type,@Remark,@Language)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ProductName", SqlDbType.NVarChar,200),
					new SqlParameter("@ProductDesc", SqlDbType.NText),
					new SqlParameter("@PicUrl", SqlDbType.NVarChar,300),
					new SqlParameter("@Price", SqlDbType.Money,8),
					new SqlParameter("@ProductNum", SqlDbType.Int,4),
					new SqlParameter("@Unit", SqlDbType.NVarChar,10),
					new SqlParameter("@Size", SqlDbType.NVarChar,300),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,100),
					new SqlParameter("@Language", SqlDbType.NChar,10)};
            parameters[0].Value = model.ProductName;
            parameters[1].Value = model.ProductDesc;
            parameters[2].Value = model.PicUrl;
            parameters[3].Value = model.Price;
            parameters[4].Value = model.ProductNum;
            parameters[5].Value = model.Unit;
            parameters[6].Value = model.Size;
            parameters[7].Value = model.Type;
            parameters[8].Value = model.Remark;
            parameters[9].Value = model.Language;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TransFormer.Model.Product model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Product set ");
            strSql.Append("ProductName=@ProductName,");
            strSql.Append("ProductDesc=@ProductDesc,");

            strSql.Append("Price=@Price,");
            strSql.Append("ProductNum=@ProductNum,");
            strSql.Append("Unit=@Unit,");
           
            strSql.Append("Type=@Type,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("Language=@Language");
            
            List<SqlParameter> parameters = new List<SqlParameter>(){
					new SqlParameter("@ProductName", SqlDbType.NVarChar,200),
					new SqlParameter("@ProductDesc", SqlDbType.NText),
					//new SqlParameter("@PicUrl", SqlDbType.NVarChar,300),
					new SqlParameter("@Price", SqlDbType.Money,8),
					new SqlParameter("@ProductNum", SqlDbType.Int,4),
					new SqlParameter("@Unit", SqlDbType.NVarChar,10),
					//new SqlParameter("@Size", SqlDbType.NVarChar,300),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,100),
					new SqlParameter("@Language", SqlDbType.NChar,10),
					new SqlParameter("@ProductId", SqlDbType.Int,4)};
            parameters[0].Value = model.ProductName;
            parameters[1].Value = model.ProductDesc;
            //parameters[2].Value = model.PicUrl;
            parameters[2].Value = model.Price;
            parameters[3].Value = model.ProductNum;
            parameters[4].Value = model.Unit;
            //parameters[6].Value = model.Size;
            parameters[5].Value = model.Type;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.Language;
            parameters[8].Value = model.ProductId;

            if (!string.IsNullOrEmpty(model.PicUrl))
            {
                strSql.Append(",PicUrl=@PicUrl");
                parameters.Add(new SqlParameter("@PicUrl", model.PicUrl));
            }
            if (!string.IsNullOrEmpty(model.Size))
            {
                
                strSql.Append(",Size=@Size");
                parameters.Add(new SqlParameter("@Size", model.Size));
            }
            strSql.Append(" where ProductId=@ProductId");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters.ToArray());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ProductId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Product ");
            strSql.Append(" where ProductId=@ProductId");
            SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)
			};
            parameters[0].Value = ProductId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ProductIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Product ");
            strSql.Append(" where ProductId in (" + ProductIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TransFormer.Model.Product GetModel(int ProductId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ProductId,ProductName,ProductDesc,PicUrl,Price,ProductNum,Unit,Size,Type,Remark,Language from Product ");
            strSql.Append(" where ProductId=@ProductId");
            SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)
			};
            parameters[0].Value = ProductId;

            TransFormer.Model.Product model = new TransFormer.Model.Product();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TransFormer.Model.Product DataRowToModel(DataRow row)
        {
            TransFormer.Model.Product model = new TransFormer.Model.Product();
            if (row != null)
            {
                if (row["ProductId"] != null && row["ProductId"].ToString() != "")
                {
                    model.ProductId = int.Parse(row["ProductId"].ToString());
                }
                if (row["ProductName"] != null)
                {
                    model.ProductName = row["ProductName"].ToString();
                }
                if (row["ProductDesc"] != null)
                {
                    model.ProductDesc = row["ProductDesc"].ToString();
                }
                if (row["PicUrl"] != null)
                {
                    model.PicUrl = row["PicUrl"].ToString();
                }
                if (row["Price"] != null && row["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(row["Price"].ToString());
                }
                if (row["ProductNum"] != null && row["ProductNum"].ToString() != "")
                {
                    model.ProductNum = int.Parse(row["ProductNum"].ToString());
                }
                if (row["Unit"] != null)
                {
                    model.Unit = row["Unit"].ToString();
                }
                if (row["Size"] != null)
                {
                    model.Size = row["Size"].ToString();
                }
                if (row["Type"] != null)
                {
                    model.Type = row["Type"].ToString();
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["Language"] != null)
                {
                    model.Language = row["Language"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ProductId,ProductName,ProductDesc,PicUrl,Price,ProductNum,Unit,Size,Type,Remark,Language ");
            strSql.Append(" FROM Product ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ProductId,ProductName,ProductDesc,PicUrl,Price,ProductNum,Unit,Size,Type,Remark,Language ");
            strSql.Append(" FROM Product ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Product ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ProductId desc");
            }
            strSql.Append(")AS Row, T.*  from Product T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Product";
            parameters[1].Value = "ProductId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        public DataSet GetProductList(string productTypeId, string productName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ProductId,ProductName,ProductDesc,PicUrl,Price,ProductNum,Unit,Size,Type,Remark,Language ");
            strSql.Append(" FROM Product where 1=1 ");
            List<SqlParameter> sqlParameters = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(productTypeId))
            {
                strSql.Append(" and productId=@pId");
                sqlParameters.Add(new SqlParameter("@pId", productTypeId));
            }
            if (!string.IsNullOrEmpty(productName))
            {
                strSql.Append(" and productName Like @pName");
                sqlParameters.Add(new SqlParameter("@pName", productName));
            }

            return DbHelperSQL.Query(strSql.ToString(), sqlParameters.ToArray());

        }
        #endregion  ExtensionMethod
    }
}

